﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="panelcommand.js"/>
/// <reference path="vswd-ext_2.2.js" />
///国标参数设置窗体

function ParamObj(paramid, paramEditObject) {
    this.ParamID = paramid;
    this.ParamEditObject = paramEditObject;
}

function GBTrackerSettingsWindow(trackername,paramdata) {
    var paramobjArray = [];
    paramobjArray.length = 0x84;
    var gbparamstore = new Ext.data.SimpleStore({
        fields: ['cmdid', 'text'],
        data: [
        [0x01, "终端心跳发送间隔"]
        , [0x02, "TCP消息应答超时时间"]
        , [0x03, "TCP消息重传次数"]
        , [0x04, "UDP消息应答超时时间"]
        , [0x05, "UDP消息重传次数"]
        , [0x06, "SMS消息应答超时时间"]
        , [0x07, "SMS消息重传次数"]
        , [0x10, "主服务器APN"]
        , [0x11, "主服务器无线通信拨号用户名"]
        , [0x12, "主服务器无线通信拨号密码"]
        , [0x13, "主服务器地址,IP或域名"]
        , [0x14, "备用服务器APN"]
        , [0x15, "备用服务器无线通信拨号用户名"]
        , [0x16, "备用服务器无线通信拨号密码"]
        , [0x17, "备用服务器地址,IP或域名"]
        , [0x18, "服务器TCP端口"]
        , [0x19, "服务器UDP端口"]
        , [0x20, "位置汇报策略"]
        , [0x21, "位置汇报方案"]
        , [0x22, "驾驶员未登录汇报时间间隔"]
        , [0x27, "休眠时汇报时间间隔"]
        , [0x28, "紧急报警时汇报时间间隔"]
        , [0x29, "缺省时间汇报间隔"]
        , [0x2c, "缺省距离汇报间隔"]
        , [0x2d, "驾驶员未登录汇报距离间隔"]
        , [0x2e, "休眠时汇报距离间隔"]
        , [0x2f, "紧急报警时汇报距离间隔"]
        , [0x30, "拐点补传角度"]
        ,[0x31,"电子围栏半径"]
        , [0x40, "监控平台电话号码"]
        , [0x41, "复位电话号码"]
        , [0x42, "恢复出厂设置电话号码"]
        , [0x43, "监控平台SMS电话号码"]
        , [0x44, "接收终端SMS文本报警号码"]
        , [0x45, "终端电话接听策略"]
        , [0x46, "每次最长通话时间"]
        , [0x47, "当月最长通话时间"]
        , [0x48, "监听电话号码"]
        , [0x49, "监管平台特权短信号码"]
        //[0x4a-0x4F] 保留
        , [0x50, "报警屏蔽字"]
        , [0x51, "报警发送文本SMS开关"]
        , [0x52, "报警拍摄开关"]
        , [0x53, "报警拍摄存储标准"]
        , [0x54, "关键标志"]
        , [0x55, "最高速度"]
        , [0x56, "超速持续时间"]
        , [0x57, "连续驾驶时间门限"]
        , [0x58, "当天累计驾驶时间门限"]
        , [0x59, "最小休息时间"]
        , [0x5A, "最长停车时间"]
        //[0x5B-0x6F] 保留
        , [0x70, "图像视频质量"]
        , [0x71, "亮度"]
        , [0x72, "对比度"]
        , [0x73, "饱和度"]
        , [0x74, "色度"]
        //[0x75-0x7F] 保留
        , [0x80, "车辆里程表读数"]
        , [0x81, "车辆所在省域ID"]
        , [0x82, "车辆所在市域ID"]
        , [0x83, "机动车号牌"]
        , [0x84, "车牌颜色"]
       ]
    });

    Ext.ms02data.alarmtypedata = [
            [0, "紧急报警"],
            [1, "超速报警"],
            [2, "疲劳驾驶"],
            [3, "预警"],
            [4, "GNSS模块发生故障"],
            [5, "GNSS天线未接"],
            [6, "GNSS天线短路"],
            [7, "终端主电源欠压"],
            [8, "终端主电源掉电"],
            [9, "终端LCD或者显示器故障"],
            [10, "TSS模块故障"],
            [11, "摄像头故障"],
            [18, "当天累计驾驶超时"],
            [19, "超时停车"],
            [20, "进出区域"],
            [21, "进出路线"],
            [22, "路段行驶时间不足/过长"],
            [23, "路线偏离报警"],
            [24, "车辆VSS故障"],
            [25, "车辆油量异常"],
            [26, "车辆被盗"],
            [27, "车辆非法点火"],
            [28, "车辆非法位移"],
            [29, "碰撞侧翻报警"]
        ];
        
    

    function createNewAlarmStore() {
       return  new Ext.data.SimpleStore({
            fields: [{ name: 'valueid', type: 'int' }, 'val'],
            data: Ext.ms02data.alarmtypedata 
        });
    }
    
    function createNewMultiParamCmbFieldForGB(pstore, labelcap, etext) {
        var pcmbfield = new Ext.ux.form.CheckboxCombo({
            //editable: false,
            labelStyle: 'padding-top:6px;padding-left:10px;text-align:left',
            fieldLabel: labelcap,
            store: pstore,
            valueField: 'valueid',
            displayField: 'val',
            //typeAhead: false,
            mode: 'local',
            //forceSelection: true,
            //triggerAction: 'all',
            //emptyText:_TrackerMain_SelectACommand,
            //selectOnFocus: true,
            width: 550
        });
        if (etext == null)
            pcmbfield.emptyText = _TrackerMain_SelectACommand;
        else
            pcmbfield.emptyText = etext;
        return pcmbfield;
    }
    
    function createNewParam(paramfieldtype, captext, maxlen, unittext, cmbstore) {
        var r;
        if (unittext.length == 0)
            unittext = " "; //give a space if text is empty
        var lblunit = createNewParamUnitLabel(unittext);
        if (paramfieldtype == EnumParamFieldType.TextField)
            r = createNewParamTextField(captext, maxlen);
        else if (paramfieldtype == EnumParamFieldType.NumberField)
            r = createNewParamNumberField(captext, maxlen);
        else if (paramfieldtype == EnumParamFieldType.ComboBoxField) {
            r = createNewParamCmbField(cmbstore, captext);
            r.setValue('0');
        }
        else if (paramfieldtype == EnumParamFieldType.MultiComboBoxField) {
            r = createNewMultiParamCmbFieldForGB(cmbstore, captext);
        }
        return r;
    }

    function createParamEditObjectByParamID(paramid) {
        if (paramid == 0x01) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x02) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", "", null);
        }
        else if (paramid == 0x03) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(次)", 9, "", null);
        }
        else if (paramid == 0x04) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x05) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(次)", 9, "", null);
        }
        else if (paramid == 0x06) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x07) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(次)", 9, "", null);
        }
        else if (paramid == 0x10) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x11) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x12) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x13) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x14) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x15) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x16) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x17) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x18) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid), 9, "", null);
        }
        else if (paramid == 0x19) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid), 9, "", null);
        }
        else if (paramid == 0x20) {
            return createNewParam(EnumParamFieldType.ComboBoxField, getCommandStringFromParamID(paramid), 0, "", new Ext.data.ArrayStore({
                    fields: [{ name: 'valueid', type: 'integer' }, 'val'],
                    data: [
                                [0, "定时汇报"],
                                [1, "定距汇报"],
                                [2, "定时和定距汇报"]
                            ]
                }));
        }
        else if (paramid == 0x21) {
            return createNewParam(EnumParamFieldType.ComboBoxField, getCommandStringFromParamID(paramid), 0, "", new Ext.data.ArrayStore({
                fields: [{ name: 'valueid', type: 'integer' }, 'val'],
                data: [
                        [0, "根据ACC状态"],
                        [1, "根据登录状态和ACC状态,先判断登录状态，若登录再根据ACC状态"]
                    ]
            }));
        }
        else if (paramid == 0x22) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x27) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x28) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x29) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x2C) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(米)", 9, "", null);
        }
        else if (paramid == 0x2D) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(米)", 9, "", null);
        }
        else if (paramid == 0x2E) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(米)", 9, "", null);
        }
        else if (paramid == 0x2F) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(米)", 9, "", null);
        }
        else if (paramid == 0x30) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(度)", 3, "", null);
        }
        else if (paramid == 0x31) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(米)", 3, "", null);
        }
        else if (paramid == 0x40) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x41) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x42) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x43) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x44) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x45) {
            return createNewParam(EnumParamFieldType.ComboBoxField, getCommandStringFromParamID(paramid), 0, "", new Ext.data.ArrayStore({
                fields: [{ name: 'valueid', type: 'integer' }, 'val'],
                data: [
                        [0, "自动接听"],
                        [1, "ACC ON时自动接听,OFF时手动接听"]
                    ]
            }));
        }
        else if (paramid == 0x46) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x47) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x48) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x49) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x50) {
            return createNewParam(EnumParamFieldType.MultiComboBoxField, getCommandStringFromParamID(paramid), 0, "", createNewAlarmStore());
        }
        else if (paramid == 0x51) {
            //SMS，报警屏蔽
        return createNewParam(EnumParamFieldType.MultiComboBoxField, getCommandStringFromParamID(paramid), 0, "", createNewAlarmStore());
        }
        else if (paramid == 0x52) {
            //拍摄,报警屏蔽
        return createNewParam(EnumParamFieldType.MultiComboBoxField, getCommandStringFromParamID(paramid), 0, "", createNewAlarmStore());
        }
        else if (paramid == 0x53) {
            //拍摄存储，报警屏蔽
        return createNewParam(EnumParamFieldType.MultiComboBoxField, getCommandStringFromParamID(paramid), 0, "", createNewAlarmStore());
        }
        else if (paramid == 0x54) {
            //位置汇报，报警屏蔽
        return createNewParam(EnumParamFieldType.MultiComboBoxField, getCommandStringFromParamID(paramid), 0, "", createNewAlarmStore());    
        }
        else if (paramid == 0x55) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(千米/小时)", 9, "", null);
        }
        else if (paramid == 0x56) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x57) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x58) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x59) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x5A) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(秒)", 9, "", null);
        }
        else if (paramid == 0x70) {
            return createNewParam(EnumParamFieldType.ComboBoxField, getCommandStringFromParamID(paramid) + "(1-10)", 0, "", getIndexStore(1, 10));
        }
        else if (paramid == 0x71) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(0-255)", 3, "", null);
        }
        else if (paramid == 0x72) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(0-127)", 3, "", null);
        }
        else if (paramid == 0x73) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(0-127)", 3, "", null);
        }
        else if (paramid == 0x74) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(0-255)", 3, "", null);
        }
        else if (paramid == 0x80) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(0.1km)", 9, "", null);
        }
        else if (paramid == 0x81) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid), 5, "", null);
        }
        else if (paramid == 0x82) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid), 5, "", null);
        }
        else if (paramid == 0x83) {
            return createNewParam(EnumParamFieldType.TextField, getCommandStringFromParamID(paramid), 32, "", null);
        }
        else if (paramid == 0x84) {
            return createNewParam(EnumParamFieldType.NumberField, getCommandStringFromParamID(paramid) + "(0-255)", 3, "", null);
        }
    }

    function addNewParamByParamID(paramid) {
        var parameditobject = createParamEditObjectByParamID(paramid);
        panelParams.add(parameditobject);
        panelParams.doLayout();

        var paramobj = new ParamObj(paramid, parameditobject);
        return paramobj;
    }

    function addNewParamByParamIDAndValue(paramid, value) {
        var parameditobject = createParamEditObjectByParamID(paramid);
        panelParams.add(parameditobject);
        panelParams.doLayout();

        var paramobj = new ParamObj(paramid, parameditobject);
        parameditobject.setValue(value);
        return paramobj;
    }
    
    var paramCombo = new Ext.ux.form.CheckboxCombo({
        store: gbparamstore,
        mode: 'local',
        displayField: 'text',
        width: 230,
        valueField: 'cmdid',
        enableKeyEvents: true,
        editable: true
    });

    
    function getCommandStringFromParamID(paramid){
      var r="";
      gbparamstore.each(function(obj){
        if (obj.data.cmdid == paramid) {
          r = obj.data.text;
        }
      });
      return r;
    }

    function getCommandIDFromString(str) {
        var r = 0;

        gbparamstore.each(function(obj) {
        if (str.indexOf(obj.data.text,0) != -1) {
                r = obj.data.cmdid;
            }
        });
        return r;
    }
   
    
    paramCombo.on('keyup', function() {
        gbparamstore.filter('text', paramCombo.getRawValue(), true, false);
        paramCombo.bindStore(gbparamstore);
    });

    paramCombo.on('blur', function() {
        gbparamstore.clearFilter();
        paramCombo.bindStore(gbparamstore);
    });

    var btnset = new Ext.Button({ text: "设置参数",
        handler: function() {
            var i;
            var cmd = EnumGPRSCommand.NotSupportThisCommand;
            var param = "";
            var mt88cmd = "";
            var mt88param = "";
            var gbcommand = null;
            var gbparamdata = paramobjArray.length.toString(16);
            for (i = 0; i < paramobjArray.length; i++) {
                var paramid = getCommandIDFromString(paramobjArray[i].ParamEditObject.fieldLabel);
                var strPID = UInt32ToDWORDString(paramid);
                var strData;
                var strParamLen = "";
                if ((paramid >= 0x00 && paramid <= 0x07) || (paramid >= 0x18 && paramid <= 0x30) || (paramid >= 0x45 && paramid <= 0x47) || (paramid >= 0x55 && paramid <= 0x80)) {
                    strData = UInt32ToDWORDString(paramobjArray[i].ParamEditObject.getValue());
                    strParamLen = "04";
                }

                else if ((paramid >= 0x10 && paramid <= 0x17) || (paramid >= 0x40 && paramid <= 0x44) || paramid == 0x48 || paramid == 0x49 || paramid == 0x83) {
                    strData = encodeGBK(paramobjArray[i].ParamEditObject.getValue());
                    if (strData.length > 0) {
                        strParamLen = padLeft(parseInt(strData.split(',').length, 10).toString(16), '0', 2).toUpperCase();
                    }
                    else
                        strParamLen = "00";
                }
                else if (paramid == 0x31 || paramid == 0x81 || paramid == 0x82) {
                    strData = UInt16ToWORDString(parseInt(paramobjArray[i].ParamEditObject.getValue()));
                    strParamLen = "02";
                }
                else if (paramid >= 0x50 && paramid <= 0x54) {
                    // var setBit
                    var splitbit = paramobjArray[i].ParamEditObject.getValue().split(',');
                    var strAlarmBin = padLeft('', '0', 32);
                    var j;
                    for (j = 0; j < splitbit.length; j++) {
                        strAlarmBin = bitSet(strAlarmBin, parseInt(splitbit[j], 10), '1');
                    }
                    strData = binStringToDWORDString(strAlarmBin);
                    strParamLen = "04";
                }
                else if (paramid == 0x84) {
                    strData = parseInt(paramobjArray[i].ParamEditObject.getValue()).toString(16);
                    strParamLen = "01";
                }
                if (strData.length > 0)
                    gbparamdata += "," + strPID + "," + strParamLen + "," + strData;
                else
                    gbparamdata += "," + strPID + "," + strParamLen;
            }

            var gbcommand = "8103";
            //gbparamdata = gbparamdata.trimStart(',');
            alert(gbparamdata);
            addTCPOrSMSCommand('HandlerSendCommand', cmd, param, mt88cmd, mt88param, gbcommand, gbparamdata);
        }
    });
    var panelParams = new Ext.Panel({
        //style: 'padding-top:10px',
        layout: 'form',
        border: false,
        autoScroll: true,
        //layoutConfig: { columns: 2 },
        items: [],
        buttons: [btnset]
        });

    var tbar = new Ext.Toolbar({
        items: ["选择参数:",
            paramCombo,
            {
                tooltip: { text: "添加选定参数", autoHide: true },
                icon: 'images/add.png',
                handler: function() {
                    //alert(paramCombo.getValue());
                    panelParams.removeAll();
                    paramobjArray = [];
                    paramobjArray.length = 0;
                    var selected = paramCombo.getValue().split(',');
                    var i;
                    for (i = 0; i < selected.length; i++) {
                        var paramid = parseInt(selected[i]);
                        paramobjArray.push(addNewParamByParamID(paramid));
                    }
                }
            }]
    });


    
    var win = new Ext.Window({
        title: "国标参数设定",
        layout: 'fit',
        height: 490,
        width: 800,
        items: [panelParams],
        tbar: tbar,
        iconCls: '',
        closeAction: 'close',
        minimizable: true,
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(win);
            }
        }

    });

    if (typeof (paramdata) != 'undefined') {
        win.setTitle("国标参数读取(" + trackername + ")");
        var ssp = paramdata.split('|');
        var i;
        for (i = 0; i < (ssp.length-1) / 2; i++) {
            var paramid = ssp[i * 2];
            var paramvalue = ssp[i * 2 + 1];
            addNewParamByParamIDAndValue(parseInt(paramid,16), paramvalue);
        }
        btnset.setVisible(false);
    }
    win.show();
}